import { Text, useMessage } from 'alemonjs'
import { selects } from '@src/apps/index'
import { battle } from '@src/db'
import { qidToUid } from '@src/apps/tools'

export const regular = /^(!|！|\/)?(战斗面板|战斗信息)/

const response = onResponse(selects, async e => {
  const [message] = useMessage(e)
  const userId = await qidToUid(e.UserId ?? e.UserKey)

  // 查询当前用户的battle_id
  const battleInfo = await battle.findOneValue({ where: { uid: userId } })
  if (!battleInfo) {
    message.send(format(Text('\n你没有进行中的战斗！')))
    return
  }
  const battle_id = battleInfo.battle_id

  // 查询该battle_id下所有成员
  const members = await battle.findAllValues({ where: { battle_id } })
  if (!members || members.length === 0) {
    message.send(format(Text('\n未找到战斗成员信息！')))
    return
  }

  // 组装面板信息
  // 先根据阵营12排列，再根据站位123456排列
  let txt = `\n===⚔战斗面板⚔===\n战斗ID：${battle_id}`
  const camps = [1, 2]
  for (const camp of camps) {
    txt += `\n===阵营${camp}===`
    const campMembers = members
      .filter(m => Number(m.battle_camp) === camp)
      .sort((a, b) => Number(a.battle_index) - Number(b.battle_index))
    for (const m of campMembers) {
      txt += `\n【${m.name}】[${m.battle_index}]\n[生命]${m.hp}/${m.hp_max}\n[魔力]${m.mp}/${m.mp_max}\n[物攻]${m.atk}[魔攻]${m.matk}\n[物防]${m.def}[魔防]${m.mdef}\n[命中]${m.hit}[闪避]${m.dodge}\n[暴击]${m.crit}[暴伤]${m.crit_dmg}\n[暴免]${m.crit_avoid}[暴抗]${m.crit_resist}\n[速度]${m.speed}[韧性]${m.tenacity}\————————————`
    }
    txt += `\n============`
  }

  message.send(format(Text(txt)))
})

export default onResponse(selects, [response.current])
